package com.jumper.study.admin.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

import com.jumper.study.common.api.CommonResult;
import com.jumper.study.common.api.ResultCode;
import com.jumper.study.common.util.CaptchaUtil;
import com.jumper.study.common.util.RequestUtil;
import com.jumper.study.common.util.StrUtil;
import com.jumper.study.db.model.Account;
import com.jumper.study.common.util.AdminUtil;

import jakarta.servlet.http.HttpServletRequest;

import com.jumper.study.admin.bo.AdminUserDetails;
import com.jumper.study.admin.dto.req.LoginReq;
import com.jumper.study.admin.dto.req.PasswordReq;
import com.jumper.study.admin.dto.req.UserInfoReq;
import com.jumper.study.admin.dto.resp.UserInfo;
import com.jumper.study.admin.service.AccountService;

@RestController
@RequestMapping("/api/auth")
public class AuthController {
    @Autowired
    private AccountService accountService;

    @Autowired
    private CaptchaUtil captchaUtil;

    @Autowired
    private AdminUtil adminUtil;

    @PostMapping("/login")
    public CommonResult<UserInfo> login(HttpServletRequest request, @RequestBody @Validated LoginReq req) {
        if (!captchaUtil.verify(req.getCode(), RequestUtil.getIp(request))) {
            return CommonResult.failed(ResultCode.VALIDATE_FAILED.getCode(), "验证码错误");
        }
        req.setIp(RequestUtil.getIp(request));
        req.setAgent(RequestUtil.getAgent(request));
        UserInfo info = accountService.login(req);
        if (info == null) {
            return CommonResult.failed(ResultCode.UNAUTHORIZED.getCode(), "用户名或密码错误");
        }
        
        return CommonResult.success(info);
    }

    @PostMapping("/captcha")
    public CommonResult<String> captcha(HttpServletRequest request) {
        
        return CommonResult.success(captchaUtil.generate(RequestUtil.getIp(request)));
    }
    
    @GetMapping("/info")
    public CommonResult<UserInfo> info() {
        return CommonResult.success(adminUtil.getUserInfo());
    }

    @PostMapping("/info")
    public CommonResult<Boolean> updateInfo(@RequestBody @Validated UserInfoReq req) {
        AdminUserDetails detail = adminUtil.getDetails();
        Account account = detail.getAccount();
        account.setName(req.getName());
        account.setAvatar(req.getAvatar());

        boolean result = accountService.updateById(account);
        return CommonResult.success(result);
    }
    

    @PostMapping("/password")
    public CommonResult<Boolean> updatePassword(@RequestBody @Validated PasswordReq req) {
        if (!StrUtil.isEqual(req.getNewPassword(), req.getConfirmPassword())) {
            return CommonResult.failed(ResultCode.VALIDATE_FAILED.getCode(), "两次输入新密码不一致");
        }
        AdminUserDetails detail = adminUtil.getDetails();
        boolean result = accountService.updatePasswordById(detail.getAccount(), req.getOldPassword(), req.getNewPassword());
        return CommonResult.success(result);
    }
}